<template>
  <el-dialog
    title="修改调度中心流量信息"
    :close-on-click-modal="false"
    :visible.sync="visible"
    class="missionPart"
  >
    <el-form
      :model="dataForm"
      ref="dataForm"
      :rules="dataRule"
      label-width="140px"
    >
      <div class="tablePart">
        <el-form-item label="泵站:" prop="equipmentId">
          <el-select filterable v-model="dataForm.equipmentId" disabled>
            <el-option
              v-for="gItem in stationOptions"
              :key="gItem.id"
              :label="gItem.flowMeterName"
              :value="gItem.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="流量时间：" prop="time">
          <el-input v-model="dataForm.time" label="流量时间" disabled />
        </el-form-item>
        <el-form-item label="流量值：" prop="value">
          <el-input v-model="dataForm.value" label="流量值" />
        </el-form-item>
      </div>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button
        @click="dataFormSubmit"
        type="primary"
        v-if="isAuth('cms:tbFlowState:update')"
        >确定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      stationOptions: [], // 泵站信息
      dataForm: {
        equipmentId: null,
        state: null,
        time: null,
        value: null,
        id: 0
      },
      dataRule: {
        value: [{ required: true, message: "流量值不能为空", trigger: "blur" }]
      }
    };
  },
  mounted() {
    this.getStationOption(); // 获取流量计数据
  },
  methods: {
    init(id) {
      this.dataForm.id = id || null;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/cms/tbFlowState/info/${this.dataForm.id}`
            ),
            method: "get"
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm = data.tbFlowState;
            }
          });
        }
      });
    },
    // 获取泵站信息
    getStationOption() {
      this.$http({
        url: this.$http.adornUrl(`/cms/tbEquipment/combo`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.stationOptions = data.list;
        } else {
          this.stationOptions = [];
        }
      });
    },
    // 提交信息
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl("/cms/tbFlowState/update"),
            method: "post",
            data: this.$http.adornData({
              equipmentId: this.dataForm.equipmentId, // 地点泵站id
              time: this.dataForm.time,
              state: this.dataForm.state,
              id: this.dataForm.id,
              value: this.dataForm.value
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
